package com.gdut.huayan.controller;

import com.gdut.huayan.entity.dto.PageQuery;
import com.gdut.huayan.entity.dto.Result;
import com.gdut.huayan.entity.po.User;
import com.gdut.huayan.service.FileService;
import com.gdut.huayan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;

    @PreAuthorize("permitAll()")
    @GetMapping("/wechat/login")
    public Result userLogin(String code) {
        return Result.success(userService.userLogin(code));
    }

    @PreAuthorize("permitAll()")
    @GetMapping("/scan/uuid")
    public Result getUUID() {
        return Result.success(userService.getUUID());
    }

    @PreAuthorize("permitAll()")
    @GetMapping("/scan/status")
    public Result getStatus(String uuid) {
        return Result.success(userService.getStatus(uuid));
    }

    @PreAuthorize("permitAll()")
    @GetMapping("/scan/confirm")
    public Result confirmLogin(String uuid, String token) {
        userService.confirmLogin(uuid, token);
        return Result.success();
    }

    //过时
    //@GetMapping("/upload")
//    public Result getUploadLink() {
//        return Result.success(userService.getUploadLink());
//    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @GetMapping("/get")
    public Result getUserMessage() {
        return Result.success(userService.getUserMessage());
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/getList")
    public Result getUserList(@RequestBody PageQuery pageQuery) {
        return Result.success(userService.getUserList(pageQuery));
    }

    @PreAuthorize("hasAnyAuthority('NORMAL_USER','SUPER_ADMIN')")
    @PostMapping("/update")
    public Result updateUserProfile(@RequestBody User user, String requestId) {
        user.setAvatarFileId(fileService.getFileStatus(requestId, true));
        userService.updateById(user);
        return Result.success();
    }

    @PreAuthorize("hasAnyAuthority('SUPER_ADMIN')")
    @PostMapping("/update-admin")
    public Result updateUserByAdmin(@RequestBody User user, String requestId) {
        user.setAvatarFileId(fileService.getFileStatus(requestId, true));
        userService.updateByAdmin(user);
        return Result.success();
    }

}
